package com.yt.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Pattern;

/**
 * 拼音工具
 */
public class PinYinUtils {
    public static String getPingYin(String src) {
        char[] inputChar;
        inputChar = src.toCharArray();
        String[] piword;
        HanyuPinyinOutputFormat hPinyinOutputFormat = new HanyuPinyinOutputFormat();
        //大小写
        hPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //带拼音状态(toneType和charType必须成对使用，否则会异常)
        //        hPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        //        hPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        hPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder piStr = new StringBuilder();
        try {
            for (char c : inputChar) {
                // 判断是否为汉字字符
                if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                    piword = PinyinHelper.toHanyuPinyinStringArray(c, hPinyinOutputFormat);
                    piStr.append(piword[0]);
                } else
                    piStr.append(c);
            }
            return piStr.toString();
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
            return piStr.toString();
        }
    }

    // 返回中文的首字母
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString();
    }

    // 将字符串转移为ASCII码
    public static String getCnASCII(String cnStr) {
        StringBuilder strBuf = new StringBuilder();
        byte[] bGBK = cnStr.getBytes();
        for (byte b : bGBK) {
            strBuf.append(Integer.toHexString(b & 0xff));
        }
        return strBuf.toString();
    }

    //返回第一个字符的首字母
    public static String getPinYinHeadCharFirst(String str) {
        StringBuilder convert = new StringBuilder();
        if (0 < str.length() ) {
            char word = str.charAt(0);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            }
            else {
                convert.append(word);
            }
        }
        return convert.toString();
    }

    //品牌首字母，去前后空格，数字返回#，大写
    public static String getPinYinHeadCharFirstBrand(String str) {
        StringBuilder convert = new StringBuilder();
        str = str.trim();
        if (0 < str.length() ) {
            char word = str.charAt(0);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            }
            else {
                convert.append(word);
            }
        }
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        if (pattern.matcher(convert.toString()).matches()){
            return "#";
        }
        return convert.toString().toUpperCase();
    }

    public static void main(String[] args) {
        String s = getPinYinHeadCharFirstBrand(" ");
        System.out.println(s);
    }
}
